<?php
/**
 * @name UserModel
 * @desc sample数据获取类, 可以访问数据库，文件，其它系统等
 * @author root
 */
class UserModel {
    public $errno = 0;
    public $errmsg = '';
    private $_db = null;
    private $_redis = null;

    public function __construct() {
        $this->_db = new PDO("mysql:host=127.0.0.1;dbname=myapi;", "root", "root");
        $this->_redis = new redis();
        if(!$this->_redis->connect('127.0.0.1', 6379)){
            $this->errno = 2000;
            $this->errmsg = 'redis连接失败';
        }
    }

    public function login($uname, $pwd){
        $query = $this->_db->prepare("select `pwd`,`id` from `user`where `name`= ? ");
        $query->execute([$uname]);
        $ret = $query->fetchAll();
        if(!$ret || count($ret)!=1){
            $this->errno = -1003;
            $this->errmsg = '用户查找失败';
            return false;
        }
        $userInfo = $ret[0];
        if($this->_password_generate($pwd)!=$userInfo['pwd']){
            $this->errno = -1004;
            $this->errmsg = '密码错误';
            return false;
        }
        return intval($userInfo[1]);
    }

    public function register($uname, $pwd, $mobile) {
        $query = $this->_db->prepare("select count(*) as c from `user` where `name`= ? ");
        $query->execute([$uname]);
        $count = $query->fetchAll();

        if($count[0]['c']!=0){
            $this->errno = -1005;
            $this->errmsg = '用户名已存在';
            return false;
        }

        if(strlen($pwd)<8){
            $this->errno = -1006;
            $this->errmsg = '密码太短，请设置至少8位的密码';
            return false;
        }else{
            $password = $this->_password_generate($pwd);
        }

        $query = $this->_db->prepare("insert into `user`(`id`,`name`,`pwd`,`reg_time`,`mobile`) values(null, ?, ?, ?, ?)");
        $ret = $query->execute([$uname, $password, date("Y-m-d H:i:s"), $mobile]);

        if(!$ret){
            $this->errno = -1006;
            $this->errmsg = '注册失败，写入数据失败';
            return false;
        }

        return true;
    }

    private function _password_generate($password){
        $pwd = md5("salt-xxxxxxxxx-".$password);
        return $pwd;
    }

    public function sendSms($mobile){
        $key = 'FIND_PWD:'.$mobile;
        if($this->_redis->get($key)){
            $this->errno = 1100;
            $this->errmsg = '已经发送验证码';
            return false;
        }else if(!$this->_redis->setex($key, 60, rand('100000', '999999'))){
            $this->errno = 1101;
            $this->errmsg = 'redis设置验证码失败';
            return false;
        }
        return true;
    }

    public function findPwd($mobile, $smsCode, $newPwd){
        $key = 'FIND_PWD:'.$mobile;
        if(!$redisCode=$this->_redis->get($key)){
            $this->errno = 1105;
            $this->errmsg = '验证码已过期，请重新发送验证码';
        }

        if($smsCode!==$redisCode){
            $this->errno = 1106;
            $this->errmsg = '验证码错误';
        }

        $query = $this->_db->prepare("update `user` set `pwd`=? where `mobile`=?");
        $ret = $query->execute([md5($newPwd), $mobile]);
        if(!$ret){
            $this->errno = -1119;
            $this->errmsg = '找回密码修改失败';
            return false;
        }

        return true;
    }
}
